package com.ruoyi.doorcontrol.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.doorcontrol.Acs.UserControl;
import com.ruoyi.doorcontrol.domain.ThDoorControl;
import com.ruoyi.doorcontrol.domain.ThDoorcontrolUser;
import com.ruoyi.doorcontrol.service.HkUserService;
import com.ruoyi.system.service.impl.SysLogininforServiceImpl;
import org.json.JSONException;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Service
public class HkUserServiceImpl implements HkUserService {

    /**
     * 查询所有人员信息
     *
     * @return
     */
    @Override
    public AjaxResult searchUserInfo(ThDoorControl control, String employeeNo) {
        List<JSONObject> jsonObjects = null;
        Integer integer = ThDoorControlServiceImpl.loginHandleMap.get(control.getIpadress());
        if(integer == null){
            throw new CustomException(control.getNotes()+"未登录！");
        }
        try {
            jsonObjects = UserControl.searchUserInfo(integer, employeeNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(jsonObjects);
    }

    /**
     * 删除人员
     * @return
     */
    @Override
    public AjaxResult deleteUserInfo(ThDoorControl control,String employeeNo) {
        Integer integer = ThDoorControlServiceImpl.loginHandleMap.get(control.getIpadress());
        if(integer == null){
            throw new CustomException(control.getNotes()+"未登录！");
        }
        String userInfo = "";
        try {
            userInfo = UserControl.deleteUserInfo(integer, employeeNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AjaxResult ajaxResult = new AjaxResult();
        if(StringUtils.isNotBlank(userInfo)){
            if (userInfo.equals("processing")) {
                System.out.println("正在删除");
                ajaxResult.put("code",501);
                ajaxResult.put("msg","正在删除");
            } else if (userInfo.equals("success")) {
                System.out.println("删除成功");
                ajaxResult.put("code",200);
                ajaxResult.put("msg","删除成功");
            }else if(userInfo.equals("failed")){
                System.out.println("删除失败");
                ajaxResult.put("code",500);
                ajaxResult.put("msg","删除失败");
            }
            return ajaxResult;
        }
        ajaxResult.put("code",500);
        ajaxResult.put("msg","删除失败");
        return ajaxResult;
    }

    /**
     * 添加人员
     * @param doorcontrolUser
     * @return
     */
    @Override
    public AjaxResult addUser(ThDoorControl control, ThDoorcontrolUser doorcontrolUser) {
        /*if(hCNetSDK==null){
            return AjaxResult.error("请先登录");
        }*/
        Integer integer = ThDoorControlServiceImpl.loginHandleMap.get(control.getIpadress());
        if(integer == null){
            throw new CustomException(control.getNotes()+"未登录！");
        }
        AjaxResult ajaxResult = null;
        try {
            ajaxResult = UserControl.addUser(integer, doorcontrolUser);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


}
